package com.lb.threadpool;

import java.util.concurrent.*;

/**
 * @ClassName： SyncTaskTest
 * @Description： 异步线程池执行多条任务
 * @Author: jzhang
 * @Date： 2024/5/1 22:24
 * @Version： V1.0
 */
public class SyncTaskTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                5,
                10,
                15,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                new ThreadPoolExecutor.CallerRunsPolicy());

        // 异步任务
        CompletableFuture<Void> firstTaskFuture = CompletableFuture.runAsync(() -> {
           // 执行查询任务1
        }, executor);

        // 异步任务
        CompletableFuture<Void> secondTaskFuture = CompletableFuture.runAsync(() -> {
            // 执行查询任务2
        }, executor);

        // 等待全部完成，执行后续逻辑
        CompletableFuture.allOf(firstTaskFuture, secondTaskFuture).get();

        //执行业务代码逻辑

        // 完成后关闭线程池
        executor.shutdown();
    }
}
